﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using EntitiesLayer;
using System.Transactions;
using System.Data.SqlClient;
using System.Data;
namespace DAL.Repositories
{
    internal class ProgramaRepository : IRepository<ProgramaEspacial>
    {
        private IList<IEntity> insertedList;
        private IList<IEntity> updatedList;
        private IList<IEntity> deletedList;

        internal ProgramaRepository()
        {

            insertedList = new List<IEntity>();
            updatedList = new List<IEntity>();
            deletedList = new List<IEntity>();
        }

        public void Insert(ProgramaEspacial entity)
        {
            insertedList.Add(entity);
        }

        public void Delete(ProgramaEspacial entity)
        {
            deletedList.Add(entity);
        }

        public void Update(ProgramaEspacial entity)
        {
            updatedList.Add(entity);
        }

        public IEnumerable<ProgramaEspacial> GetAll()
        {
            
            SqlCommand cmd = new SqlCommand();
            DataSet ds = DBAccess.ExecuteSPWithDS(ref cmd, "pa_listar_programas");

            foreach (DataRow dr in ds.Tables[0].Rows)
                yield return crearPrograma(dr, Convert.ToInt32(dr["IDPROGRAMA"]));



        }
        public ProgramaEspacial GetById(int id) {
            

            ProgramaEspacial objPrograma = null;
            SqlCommand cmd = new SqlCommand();
            cmd.Parameters.Add(new SqlParameter("@id", id));
            DataSet ds = DBAccess.ExecuteSPWithDS(ref cmd, "pa_consultar_programa_porID ");

            foreach (DataRow dr in ds.Tables[0].Rows)
                objPrograma = crearPrograma(dr, id);


            return objPrograma;
        }
       

        public void Save()
        {
            using (TransactionScope scope = new TransactionScope())
            {

                try
                {

                    foreach (ProgramaEspacial p in insertedList)
                        InsertSatelite(p);

                    foreach (ProgramaEspacial p in updatedList)
                        UpdateSatelite(p);

                    foreach (ProgramaEspacial p in deletedList)
                        DeleteSatelite(p);


                    scope.Complete();
                }
                catch (TransactionAbortedException ex)
                {
                    Console.WriteLine(ex.Message);
                    throw ex;
                }
                catch (ApplicationException ex)
                {
                    Console.WriteLine(ex.Message);
                    throw ex;
                }
                finally
                {
                    Clear();
                }

            }
        }

        public void Clear()
        {
            insertedList.Clear();
            updatedList.Clear();
            deletedList.Clear();
        }
        #region Utilities

        internal static ProgramaEspacial crearPrograma(DataRow dr, int id)
        {
           

            ProgramaEspacial objPrograma = new ProgramaEspacial(

                        id: id,
                        nombre: dr["NOMBRE"].ToString(),
                        fecha: Convert.ToDateTime(dr["FECINICIO"]),
                        fechaFinalizacion: Convert.ToDateTime(dr["FECFINAL"]),
                        alcance: dr["ALCANCE"].ToString(),
                        missionesDelegate: new Lazy<IList<Mission>>(()=>{
                            return MissionRepository.consultarMissionesPrograma(id);
                        })
                        
                  );

            return objPrograma;
        }
        

        private void InsertSatelite(ProgramaEspacial objPrograma)
        {
            try
            {
        

                SqlCommand cmd = new SqlCommand();
                cmd.Parameters.Add(new SqlParameter("@nombre", objPrograma.Nombre));
                cmd.Parameters.Add(new SqlParameter("@fecinicio", objPrograma.Fecha));
                cmd.Parameters.Add(new SqlParameter("@fecfinal", objPrograma.FechaFinalizacion));
                cmd.Parameters.Add(new SqlParameter("@alcance", objPrograma.Alcance));
                DataSet ds = DBAccess.ExecuteSPWithDS(ref cmd, "pa_RegistrarPrograma");
            }
            catch (SqlException ex)
            {
                Console.WriteLine(ex.Message);
                throw ex;
            }

        }

        private void UpdateSatelite(ProgramaEspacial objSatelite)
        {
            throw new NotImplementedException();

        }

        private void DeleteSatelite(ProgramaEspacial objSatelite)
        {
            throw new NotImplementedException();

        }
    }
        #endregion Utilities
}